import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-10-30
 * Time: 19:54
 */
public class Test {
    public static void main(String[] args) {
       int[] array = {19,34,5,67,45,12,7,4,8};
//       quickSort(array);
//        heapSort(array);
        merger(array);
        System.out.println(Arrays.toString(array));
    }

    private static void merger(int[] array) {
        mergeChild(array,0,array.length-1);
    }

    private static void mergeChild(int[] array, int left, int right) {
        if(left == right){
            return;
        }
        int mid = (left + right)/2;
        mergeChild(array,left,mid);
        mergeChild(array,mid+1,right);
        merge(array,left,mid,right);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        int k = 0;
        int[] tmp = new int[right-left+1];
        while (s1 <= e1 && s2 <= e2){
            if(array[s1] < array[s2]){
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2){
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }

    private static void heapSort(int[] array) {
        createBigheap(array);
        int end = array.length-1;
        while (end > 0){
            swap(array,0,end);
            shifDown(array,0,end);
            end--;
        }
    }

    private static void createBigheap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0  ; parent--) {
            shifDown(array,parent,array.length);
        }
    }

    private static void shifDown(int[] array, int parent, int len) {
        int child = parent *2 +1;
        while (child < len){
            if(child +1 <len && array[child +1]> array[child]){
                child ++;
            }
            if(array[parent] < array[child]){
                swap(array,parent,child);
                parent = child;
                child = 2*parent +1;
            }else {
                break;
            }
        }
    }

    private static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int left, int right) {
        if(left >= right){
            return;
        }
        int pivot = Hare(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }

    private static int Hare(int[] array, int left, int right) {
        int pivot = array[left];
        int i = left;
        while (left < right){
            while (left < right && array[right] >= pivot){
                right--;
            }
            while (left < right && array[left] <= pivot){
                left++;
            }
            swap(array,right,left);
        }
        swap(array,i,left);
        return left;
    }

    private static void swap(int[] array, int right, int left) {
        int tmp = array[left];
        array[left] = array[right];
        array[right] = tmp;
    }
}
